package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessage;
import com.erpoa.entity.PackMessageByFlatStorage;
import com.erpoa.entity.PackMessageByMerge;
import com.erpoa.service.BatchMergeService;
import com.erpoa.service.FlatStorageService;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;


/**
 * @description:
 * @author: jamin.chen
 * @createDate: 2020/1/3 9:37
 * @version: 1.0
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "flatStorageController", tags = {"批次条码合并接口"})
@RestController
@RequestMapping("/PDAService")
public class BatchMergeController {
    private static final Logger logger = LoggerFactory.getLogger(BatchMergeController.class);


    @Autowired
    private BatchMergeService batchMergeService;


    /**
     * batchMergeServlet
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("批次条码合并接口")
    @RequestMapping(value = {"/batchMergeServlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DealMessage batchMergeServlet(@RequestBody PackMessageByMerge packMessage) {
        DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();

        try {
            logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            switch (optType) {
                //权限接口
                case 0:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.querytcPgenFile(packMessage)));
                    break;

                //单号接口
                case 1:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.selectBatchMergeToday(packMessage)));
                    break;
                //生成合并单号
                case 2:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.selectSequnce(packMessage)));
                    break;
                //根据合并条码获取子页面条码数据
                case 3:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.selectBatchMergeLine(packMessage)));
                    break;

                //根据合并条码获取和扫描条码获取数据
                case 4:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.selectBatchMergeLineByScan(packMessage)));
                    break;
                //保存或者提交
                case 5:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.updateMergeLineByScan(packMessage)));
                    break;
                //删除
                case 6:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.delMergeLineByScan(packMessage)));
                    break;
                //根据合并单号获取新批次号
                case 7:
                    dealMessage.setMessage(JSON.toJSONString(batchMergeService.selectBatchNoByPackmessage(packMessage)));
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof DuplicateKeyException) {
                throw new PDAException("数据重复！");

            } else if (e instanceof SQLException) {
                throw new PDAException("数据重复！");
            } else {
                dealMessage = new DealMessage(false, "数据处理出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }


}
